package com.cardbaobao.cardbabyclient.ui.adapter;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.callback.ItemClickedCallback;
import com.cardbaobao.cardbabyclient.model.CreditCardBank;
import com.cardbaobao.cardbabyclient.model.UpperQuota;
import com.cardbaobao.cardbabyclient.utils.DensityUtil;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Jiangzx on 2017/7/4 15:26.
 */

public class UpperQuotaAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int COUNT_PER_ROW=4;//每行4个
    private ItemClickedCallback<UpperQuota.Bean> clickedCallback;
    public void setItemClickedCallback(ItemClickedCallback<UpperQuota.Bean> clickedCallback){
        this.clickedCallback=clickedCallback;
    }

    private List<UpperQuota.Bean> data;
    public UpperQuotaAdapter(List<UpperQuota.Bean> data){
        this.data=data;
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView= LayoutInflater.from(parent.getContext()).
                inflate(R.layout.item_upper_quota,parent,false);
        return new Holder(itemView);
    }

    public void setData(List<UpperQuota.Bean> data){
        this.data=data;
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final UpperQuota.Bean bean=data.get(position);
        if(holder instanceof Holder){
            Holder bankHolder= (Holder) holder;
            bankHolder.text.setText(bean.getName());
            ImageLoaderUtil.displayImageWithPlaceHolder(URLUtil.CBB_IMG_URL+bean.getPicture_path(),
                    bankHolder.image,R.mipmap.img_default_square);
            if(isLastRow(position)){
                ViewGroup.MarginLayoutParams layoutParams=
                        (ViewGroup.MarginLayoutParams) bankHolder.text.getLayoutParams();
                layoutParams.bottomMargin= DensityUtil.dp2px(20);
                bankHolder.text.setLayoutParams(layoutParams);
            }
            bankHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(clickedCallback!=null){
                        clickedCallback.onItemClicked(bean);
                    }
                }
            });
        }

    }

    private boolean isLastRow(int position){
        boolean isLastRow=false;
        int itemCount=getItemCount();
        int lines;//总行数
        if(itemCount%COUNT_PER_ROW==0){
            lines=itemCount/COUNT_PER_ROW;
        }else{
            lines=itemCount/COUNT_PER_ROW+1;
        }
        if((position/COUNT_PER_ROW)+1==lines){
            isLastRow=true;
        }
        return isLastRow;
    }

    @Override
    public int getItemCount() {
        return data==null?0:data.size();
    }

    private class Holder extends RecyclerView.ViewHolder{
        private CircleImageView image;
        private TextView text;
        private Holder(View itemView){
            super(itemView);
            image= (CircleImageView) itemView.findViewById(R.id.bank_logo);
            text= (TextView) itemView.findViewById(R.id.title);
        }
    }
}
